home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- /*
- *
- * gloverlay.c - draw a red grid in the overlay planes
- * - rotate the rainbox cube in the standard planes
- * via a rotate button widget.
- *
- * Ivan Hajadi
- * 12-June-1992
- *
- * to compile:
- * cc -o gloverlay gloverlay.c -lXirisw -lgl_s -lXm -lXt -lX11_s -lPW -lsun
- */
-
- /* Motif includes */
- #include <Xm/Xm.h>
- #include <Xm/Form.h>
- #include <Xm/PushB.h>
-
- /* GL widget include */
- #include <X11/Xirisw/GlxMDraw.h>
-
- #include <stdio.h>
-
-
- GLXconfig rgb_mode[] = {
- { GLX_NORMAL, GLX_RGB, TRUE },
- { GLX_NORMAL, GLX_DOUBLE, TRUE },
- { GLX_NORMAL, GLX_ZSIZE, GLX_NOCONFIG },
- { GLX_OVERLAY, GLX_BUFSIZE, 2},
- { 0, 0, 0, }
- };
-
- /* globals */
- XtAppContext app_context;
- XtWorkProcId work_procid = 0;
- Display *display;
- Window normalWindow;
- Window overlayWindow;
- Pixel overlay_red_pixel;
-
- void drawscene(int, int);
- void drawOverlayScene();
- void installColormaps(Widget, Widget);
-
- main(int argc, char** argv)
- {
- Widget top, form, quit_but, anim_but;
- Widget gl_widget;
- int overlayPlns;
-
- /* Prototypes */
-
- void quitCB(Widget, XtPointer, XtPointer);
- void ginitCB(Widget, XtPointer, XtPointer);
- void exposeCB(Widget, XtPointer, XtPointer);
- void overlayExposeCB (Widget, XtPointer, XtPointer);
- void animateCB(Widget, XtPointer, XtPointer);
- void set_overlaymap( Widget );
-
- Arg args[20];
- int n;
-
- top = XtAppInitialize(&app_context, "Cube",
- NULL, 0, &argc, argv, NULL, NULL, 0
- );
- n=0;
- XtSetArg(args[n], XmNwidth, 730); n++;
- XtSetArg(args[n], XmNheight, 445); n++;
- form = XmCreateForm(top, "form", args, n);
- XtManageChild(form);
-
- /* Create GL widget */
-
- /* Check if we have enough overlay planes */
- overlayPlns = getgdesc(GD_BITS_OVER_SNG_CMODE);
-
- if (! overlayPlns ) {
- fprintf(stderr, "No overlay planes.\n");
- exit(1);
- }
-
- n = 0;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNtopAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNleftOffset, 20); n++;
- XtSetArg(args[n], XmNrightOffset, 120); n++;
- XtSetArg(args[n], XmNbottomOffset, 40); n++;
- XtSetArg(args[n], XmNtopOffset, 40); n++;
- XtSetArg(args[n], XmNwidth, 500); n++;
- XtSetArg(args[n], XmNheight, 400); n++;
- XtSetArg(args[n], GlxNglxConfig, rgb_mode); n++;
- XtSetArg(args[n], GlxNuseOverlay, True); n++;
-
- gl_widget = GlxCreateMDraw(form, "gl_widget", args, n);
- XtManageChild(gl_widget);
-
- /* Add callbacks */
-
- XtAddCallback(gl_widget, GlxNginitCallback, ginitCB, NULL);
- XtAddCallback(gl_widget, GlxNexposeCallback, exposeCB, NULL);
- XtAddCallback(gl_widget, GlxNoverlayExposeCallback, overlayExposeCB, NULL);
-
- n=0;
- XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNrightOffset, 10); n++;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNbottomOffset, 10); n++;
- XtSetArg(args[n], XmNtraversalOn, False); n++;
- quit_but = XmCreatePushButton(form, " Quit ", args, n);
- XtManageChild(quit_but);
-
- n=0;
- XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNrightOffset, 10); n++;
- XtSetArg(args[n], XmNbottomAttachment, XmATTACH_FORM); n++;
- XtSetArg(args[n], XmNbottomOffset, 50); n++;
- XtSetArg(args[n], XmNtraversalOn, False); n++;
- anim_but = XmCreatePushButton(form, "Rotate", args, n);
- XtManageChild(anim_but);
-
- XtAddCallback(quit_but, XmNactivateCallback, quitCB, NULL);
- XtAddCallback(anim_but, XmNactivateCallback, animateCB, NULL);
-
- XtRealizeWidget(top);
- set_overlaymap( gl_widget );
-
- /* Time to install the colormaps */
- installColormaps(top, gl_widget);
-
- /* Main Loop */
- XtAppMainLoop(app_context);
- }
-
- void installColormaps(Widget toplevel_widget, Widget gl_widget)
- {
- Window window_list[3];
-
- display = XtDisplay(toplevel_widget);
- window_list[0] = overlayWindow;
- window_list[1] = XtWindow(toplevel_widget);
- window_list[2] = XtWindow(gl_widget);
- XSetWMColormapWindows(display, XtWindow(toplevel_widget), window_list, 3);
- }
-
- Boolean animate(XtPointer clientD)
- {
- static int angle_x = 0, angle_y = 0;
- angle_y += 75;
- angle_x += 75;
-
- if ((angle_y >= 3600) || (angle_y <= -3600))
- angle_y = 0;
- if ((angle_x >= 3600) || (angle_x <= -3600))
- angle_x = 0;
-
- GLXwinset(display, normalWindow);
- drawscene(angle_x, angle_y);
-
- /*
- Return False so this WorkProc
- keeps getting called.
- */
- return False;
- }
-
- void animateCB(Widget w, XtPointer clientD, XtPointer callD)
- {
- static Boolean started = False;
- Arg args[1];
- XmString xms;
-
- if (! started ) {
- Boolean animate(XtPointer);
- work_procid = XtAppAddWorkProc(app_context, animate, NULL);
- started = True;
- xms = XmStringCreateSimple(" Stop ");
- } else {
- XtRemoveWorkProc(work_procid);
- started = False;
- xms = XmStringCreateSimple("Rotate");
- }
-
- /* Changed the button label */
- XtSetArg(args[0], XmNlabelString, xms);
- XtSetValues(w, args, 1);
- XmStringFree(xms);
- }
-
- void ginitCB(Widget gl_widget, XtPointer clientD, XtPointer callD)
- {
- GlxDrawCallbackStruct *call_data = (GlxDrawCallbackStruct *) callD;
-
- display = XtDisplay(gl_widget);
- normalWindow = call_data->window;
- GLXwinset(display, normalWindow);
-
- shademodel(GOURAUD);
- zbuffer(TRUE);
- subpixel(TRUE);
- lsetdepth(getgdesc(GD_ZMIN), getgdesc(GD_ZMAX));
- perspective(450, 1, 1, 100);
- }
-
- void set_overlaymap( Widget gl_widget )
- {
- /* Create and setup overlay colormap entries */
-
- Colormap overlay_cmap;
- XColor color;
- unsigned long pixels_return[ 1 ];
- XVisualInfo *gl_visual_info;
-
- XtVaGetValues(gl_widget,
- GlxNoverlayWindow, &overlayWindow,
- GlxNoverlayVisual, &gl_visual_info,
- NULL);
- overlay_cmap = XCreateColormap(display, overlayWindow,
- gl_visual_info->visual, AllocNone );
-
- XtVaSetValues( gl_widget, GlxNoverlayColormap, overlay_cmap, NULL );
-
- /* Allocate some color cells */
- if (!XAllocColorCells( display, overlay_cmap, False, NULL, 0,
- pixels_return, 1 )) {
- fprintf( stderr, "XAllocColorCells failed.\n" );
- exit( 1 );
- }
-
- /* set the color cell to red */
- overlay_red_pixel = color.pixel = pixels_return[ 0 ];
- color.blue = 0;
- color.green = 0;
- color.red = 0xffff;
- color.flags = DoRed|DoGreen|DoBlue;
- XStoreColor( display, overlay_cmap, &color );
-
- }
-
- void exposeCB(Widget w, XtPointer clientD, XtPointer callD)
- {
- GlxDrawCallbackStruct* call_data = (GlxDrawCallbackStruct*) callD;
- GLXwinset(XtDisplay(w), call_data->window);
- animate(NULL);
- }
-
- void overlayExposeCB(Widget w, XtPointer clientD, XtPointer callD)
- {
- GlxDrawCallbackStruct* call_data = (GlxDrawCallbackStruct*) callD;
- GLXwinset(XtDisplay(w), call_data->window);
- drawOverlayScene(call_data->width, call_data->height);
- }
-
- void quitCB(Widget w, XtPointer clientD, XtPointer callD)
- {
- exit(0);
- }
-
- void drawOverlayScene(int w, int h)
- {
- int xoffset = w/5;
- int yoffset = h/5;
- int x1=0,y1=0, x2=0,y2=0;
- register int i,j;
-
- pushmatrix();
- ortho2(-0.1, (Coord)w+0.1, -0.1, (Coord)h+0.1);
- reshapeviewport();
- color(0);
- clear();
-
- color(overlay_red_pixel); /* red */
-
- for (i=0;i<5;i++) {
- x2 = xoffset;
- y2 += yoffset;
- for (j=0;j<5;j++) {
- recti(x1,y1, x2,y2);
- x1 += xoffset;
- x2 += xoffset;
- }
- x1 = 0;
- y1 += yoffset;
- }
- popmatrix();
- }
-
- void drawscene(int angle_x, int angle_y)
- {
- /* Draw smooth-shaded cube */
- static long v1[4][3] = {
- {-10, -10, 10},
- {10, -10, 10},
- {10, 10, 10},
- {-10, 10, 10},
- };
- static long v2[4][3] = {
- {-10, -10, -10},
- {-10, 10, -10},
- {10, 10, -10},
- {10, -10, -10},
- };
- static long colors1[] = {
- 0x000000FF,
- 0x00FF0000,
- 0x0000FF00,
- 0x00FFFF00,
- };
- static long colors2[] = {
- 0x0000FFFF,
- 0x00FF00FF,
- 0x00FFFFFF,
- 0x0FF00000,
- };
-
- register int i;
-
- reshapeviewport();
- czclear(0x00777777, getgdesc(GD_ZMAX));
- pushmatrix();
- polarview(80, 0, 250, 0);
-
- pushmatrix();
- rotate(angle_x, 'x');
- rotate(angle_y, 'y');
-
- bgnpolygon();
- for (i=0; i<4; i++) {
- cpack(colors1[i]);
- v3i(v1[i]);
- }
- endpolygon();
- bgnpolygon();
- for(i=0; i<4; i++) {
- cpack(colors2[i]);
- v3i(v2[i]);
- }
- endpolygon();
-
-
- bgnpolygon();
- cpack(colors1[1]);
- v3i(v1[1]);
- cpack(colors1[2]);
- v3i(v1[2]);
- cpack(colors2[2]);
- v3i(v2[2]);
- cpack(colors2[3]);
- v3i(v2[3]);
- endpolygon();
- bgnpolygon();
- cpack(colors1[0]);
- v3i(v1[0]);
- cpack(colors1[3]);
- v3i(v1[3]);
- cpack(colors2[1]);
- v3i(v2[1]);
- cpack(colors2[0]);
- v3i(v2[0]);
- endpolygon();
-
- bgnpolygon();
- cpack(colors1[2]);
- v3i(v1[2]);
- cpack(colors1[3]);
- v3i(v1[3]);
- cpack(colors2[1]);
- v3i(v2[1]);
- cpack(colors2[2]);
- v3i(v2[2]);
- endpolygon();
- bgnpolygon();
- cpack(colors1[1]);
- v3i(v1[1]);
- cpack(colors1[0]);
- v3i(v1[0]);
- cpack(colors2[0]);
- v3i(v2[0]);
- cpack(colors2[3]);
- v3i(v2[3]);
- endpolygon();
-
- popmatrix();
- popmatrix();
-
- swapbuffers();
- }
-
-